$.ready((function(){
    var pnum =0;
    var fnum = 0;
    var anum = 0;
    jQuery.validator.addMethod("LevelValidate", function(value, element) {
        var obj = element;
        var result = $("input[name='is_p_m']:checked").val();
        var element = $(element);
        var nodeName = element.attr("name");
        if(nodeName == "p_start" || nodeName== "p_end"){
            if(result === "1"){
                if(nodeName == "p_start"){
                    var result = this.optional(obj) || element.val();
                    return result;
                }
                var s_value = parseInt($("select[name='p_start']").val());
                var e_value = parseInt(element.val());
                var result = this.optional(obj) || (element.val() && e_value>=s_value);
                return result;
            }
            return this.optional(obj) || true;
        }
        if(nodeName == "m_start" || nodeName== "m_end"){
            if(result === "2"){
                if(nodeName == "m_start"){
                    return this.optional(obj) || element.val();
                }
                var s_value = $("select[name='m_start']").val();
                var e_value = element.val();
                return this.optional(obj) || (element.val()&&e_value>=s_value);
            }
            return this.optional(obj) || true;
        }
    }, "请正确填写您的手机号码");

    jQuery.validator.addMethod("numNotZero", function(value, element) {

        var obj = element;
        var element = $(element);
        var result =  parseInt(element.val()) >0 ;
        return this.optional(obj) || result;

    }, "需求人数");

    jQuery.validator.addMethod("isExamination", function(value, element) {
        var code = parseInt($("input[name='is_written']:checked").val());
        if(code ===1 ){
            var exminationCode = $("input[name='written_info']").val();
            return this.optional(element) || exminationCode;
        }
        return this.optional(element) || true;


    }, "请正确填写您的手机号码");
//    $(".position").bind("change",function(){
//        var obj = $(this).children("option:selected");
//        var pnum = obj.attr("pnum")|| 0;
//        var fnum = obj.attr("fnum") || 0;
//        var anum = obj.attr("anum")||0;
//        $(".plan_num").text(pnum);
//        $(".act_num").text(fnum);
//        if((pnum-fnum)>0){
//            $(".hid_flow em").text(pnum-fnum);
//            $(".hid_flow").show();
//        }else{
//            $(".hid_flow").hide();
//        }
//
//        $(".per").trigger("blur");
//    });
    $(".per").bind("blur",function(){
        var obj = $(this);
        var num = parseInt(obj.val()) || 0;
        if(pnum > anum){
            if((anum+ num) > pnum){
                var out = anum+num-pnum;
                $("input[name='plan_num']").val(num- out);
                $("input[name='out_plan_num']").val(out);
                $("input[name='plan_num1']").val(num- out);
                $("input[name='out_plan_num1']").val(out);
                $(".tip").show();
            }
            else{
                $("input[name='plan_num']").val(num);
                $("input[name='out_plan_num']").val(0);
                $("input[name='plan_num1']").val(num);
                $("input[name='out_plan_num1']").val(0);
            }
        }
        else{
            $("input[name='plan_num']").val(0);
            $("input[name='out_plan_num']").val(num);
            $("input[name='plan_num1']").val(0);
            $("input[name='out_plan_num1']").val(num);
            $(".tip").show();
        }

    });

    $(".per").bind("keyup afterpaste",function(){
        var obj = $(this);
        var seltObj = $(this).children("option:selected");
        if(!$(".position").val()){
            obj.val("");
            $(".position").focus();
            $(".position").addClass("bcolor");
        }
        obj.val(obj.val().replace(/\D/g,''));
        $(".tip").hide();
    });
    $("textarea").bind("keydown",function(){
         //this.value = this.value.substring(0,90);
    });

    $("input[name='work_core[]']").bind('click',function(){
        var objSelected =  $("input[name='work_core[]']:checked");
        var obj = $("input[name='work_core[]']");
        var len = $("input[name='work_core[]']:checked").length;
        if(len > 0){
            var objabr = $(".tipAbility");
            objabr.html("亲您最多可选择三项!");
            objabr.css("color","#333");
        }
        if(len == 0){
            var obj = $(".tipAbility");
            obj.html("<a name='tipAbility'>亲，您至少要选择一项哦！</a>");
            obj.css("color","red");
        }
        if(len== 3){
            obj.attr("disabled","disabled");
            objSelected.attr("disabled",false);
        }else{
            obj.attr("disabled",false);
        }

    });
   if($("input[name='work_core[]']:checked").length === 3){
       $("input[name='work_core[]']:unchecked").attr("disabled","disabled");
   }
    var validator = $('form').validate({
        rules: {
            job_id: {
                required:true
            },
            job_needs: {
                required:true,
                numNotZero:true
            },
            user_type: {
                required:true
            },
            is_p_m: {
                required:true
            },
            p_start: {
                LevelValidate:true
            },
            p_end: {
                LevelValidate:true
            },
            m_start: {
                LevelValidate:true
            },
            m_end:{
                LevelValidate:true
            },
            work_address:{
                required:true
            },
            work_special:{
                required:true
            },
            work_plus:{
                required:true
            },
            work_skilled:{
                required:true
            },
            is_written:{
                isExamination:true
            },
            apply_reason:{
                required:true
            }
        },

        errorPlacement:function (error, element) {
            // element.style.textAlign="left";
           element = $(element);
            element.addClass("bcolor");
            if(element.attr("name") == "is_written"){

                $("input[name='written_info']").addClass("bcolor");
            }
        },
        success:function(error,element){
            element = $(element);
            element.removeClass("bcolor");
            if(element.attr("name") == "is_written"){

                $("input[name='written_info']").removeClass("bcolor");
            }
        }

    });
    $(".submit").bind("click",function(){
        var len = $("input[name='work_core[]']:checked").length;
        if(len == '0'){
            var obj = $(".tipAbility");
            obj.html("亲，您至少要选择一项哦！");
            obj.css("color","red");
            $("input[name='work_core[]']").focus();
            return false;
        }
        return true;


    });
    $("input[name='is_written']").bind('click',function(){
        var obj = $(this);
        if(obj.val()=="1"){
            $(".examinationBlock").slideDown();
            return;
        }
        $(".examinationBlock").hide();

    });

    jobResult = false;
    $("input[name='job_name']").autocomplete({
        source: function(request, response) {
            var ajaxUrl2 = url_job;
            var packcity = $(".city_id").find("option:selected").val();//city
            $.ajax({
                url:ajaxUrl2,
                dataType: "json",
                data: {
                    city:packcity,
                    name: request.term
                },
                success: function(data) {
                    response( $.map( data.data, function( item ) {
                        //response( $.map( testdata, function( item ) {
                        var obj = {
                            label:  item.job_name,
                            value:  item.job_name ,
                            id:  item.job_id,
                            cityID:item.city_id,
                            cityName:item.city_name
                        };
                     //   alert(obj.id);
                        return obj;
                    }));
                }
            });
        },
        select: function( event, ui ) {
            var id = ui.item.id;
            var city_id =ui.item.cityID;
            $(".city_id").val(city_id);
            $(".job_id").val(id);
            jobResult = true;
            var city =eval('(' + address_list + ')');
            var work_position = $("select[name='work_address']");
            work_position.children().remove();
            var arr = [];
            for(var i in city[city_id]){
                var str = "<option value="+i+">"+city[city_id][i]+"</option>";
                arr.push(str);
            }
            work_position.html(arr.join(""));
            jQuery.ajax({
                async:true,
                url:num_url,
                data:{'job_id':id},
                type:'post',
                success:function (data) {
                    var data = eval('(' + data + ')');

                    if(!!data.errcode){//存在员工档案
                        var data = data.data;
                        pnum = parseInt(data.pnum);//计划编制
                        fnum = parseInt(data.fnum);//在编 人数
                        anum = parseInt(data.anum);//在编人数＋正在走审批流程人数
                        $(".plan_num").html(pnum);
                        $(".act_num").html(fnum);
                        if((anum-fnum)>0){
                            $(".hid_flow").html('待审批人数<em>'+(anum-fnum)+'</em>').show();
                        }else{
                            $(".hid_flow").hide();
                        }
                        $(".per").trigger("blur");
                        return;
                    }
                    alert("无数据");

                    return;

                }
            });




        }
    });
$("input[name='job_name']").bind("keydown",function(event){
    if((event.keyCode == 13 && jobResult == true)||(event.keyCode == 9 && jobResult == true) || (event.keyCode == 32 && jobResult == true)){
        return;
    }
    jobResult = false;
});
$("input[name='job_name']").bind("blur",function(){
    if(!jobResult){
        $(".city_id").val('');
        $(".job_id").val('');
        $(this).val("");
        $(".hid_flow").children("em").html('').parent().hide();
        $(".plan_num").html('');
        $(".act_num").html('');

    }
});



})());
